1485A - Add and Divide - CodeForces Solution


brute force greedy math number theory *1000

Please click on ads to support us..

Python Code:

from cmath import inf
 
def custom_log(a : int, b : int) -> int:
    turn = 0
    if b == 1:
        b = 2
        turn += 1
    while a > 0:
        a //= b
        turn += 1
    return turn
 
for _ in range(
    int(input())
    ):
    a, b = map(int, input().split())
    ans = inf
    for i in range(100):
        ans = min(ans, (custom_log(a, b+i) + i))
    print(ans)

C++ Code:

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
typedef std::vector<long long> vi;
#define F first
#define S second
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define PI 3.14159265358979323846
#define all(a) a.begin(), a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
const ll INF = LLONG_MAX / 2;
const ll N = 2e5 + 1;
using namespace std;
int main()
{
    fast;
    ll t = 1;
    std::cin >> t;
    while (t--)
    {
        ll a, b, i, j = 0;
        std::cin >> a >> b;
        if (b > a)
        {
            cout << 1 << "\n";
        }
        else if (a == b)
        {
            cout << 2 << "\n";
        }
        else
        {
            ll ans = mod;
            if (b == 1)
            {
                fr(i, 1, 31)
                {
                    ll co = 0, d = a;
                    while (d != 0)
                    {
                        d = d / (i + b);
                        co++;
                    }
                    ans = min(ans, i + co);
                }
                cout << ans << "\n";
                continue;
            }
            fr(i, 0, 31)
            {
                ll co = 0, d = a;
                while (d != 0)
                {
                    d = d / (i + b);
                    co++;
                }
                ans = min(ans, i + co);
            }
            cout << ans << "\n";
        }
    }
}


Comments

Submit
0 Comments
More Questions

1055A - Metro
1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String
1104B - Game with string
1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum
1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves